

cc.Class({
    extends: cc.Component,

    properties: {
      bullet:cc.Prefab,
      wing_plane:cc.Prefab,
      bang:cc.Prefab,
    },

   

     onLoad () {
     this.node.hp = 2000;
     this.HP = this.node.hp;
     },

     init:function(num,type){
         //设定僚机位置
        this.posx = num;
        //设置僚机编号
        this.node.type = type;
        //生成僚机子弹
        this.schedule(this.Bullet_launch,0.2); 
               
        },
        //停止生成子弹
        stop_winplane_schedule:function(){
         this.unschedule(this.Bullet_launch);
        },
        //开始生成子弹
        begin_winplace_schedule:function(){
            this.schedule(this.Bullet_launch,0.2); 
        },
    
    //生成僚机子弹
    Bullet_launch:function(){
        var bullet1 = window.Global.getClassAndAdd(this.bullet,"Bullet",this.node.parent.parent);
        var rotation = 0;
        var position = this.node.parent.position;
        bullet1.tNode.setPosition(this.node.parent.position.x + this.node.x,position.y);
        bullet1.tClass.init(rotation,1);
    },
    
    //碰撞检测
    onCollisionEnter:function(other,self){
   
        //撞到子弹掉血  
        if(other.node.group == "enemy"){   
            if(!window.Global.Bullet_hit){
                return;
             }
            self.node.children[0].active = true;   
            if(other.node.type == 0){
                this.node.hp -= 100;
            }
            if(other.node.type == 1){
                this.node.hp -= 50;
            }
            //撞到激光
            if(other.node.type == 2){    
                this.node.hp  = 0;
                if(this.node.hp <= 0){
                    self.node.destroy();
                }
                let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent.parent); 
                bang.tNode.setPosition(self.node.position);
                return;
            }
            if(other.node.type == 3 || other.node.type == 4){
        
                this.node.hp -= 100;  
            }
            if(this.node.hp <= 0){
                self.node.destroy();
            }
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent.parent); 
            bang.tNode.setPosition(other.node.position);
            other.node.destroy();
        }
        //僚机撞到道具生成僚机
        if(other.node.group == "Meteorite"){
            
            //撞到陨石
            if(other.node.type == 0 || other.node.type == 1){
                self.node.children[0].active = true;    
                if(other.node.type == 0){
                    this.node.hp -= 100;
                }
                if(other.node.type == 1){
                    this.node.hp -= 50;
                }
                if(this.node.hp <= 0){
                    self.node.destroy();
                }
                let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent.parent); 
                bang.tNode.setPosition(other.node.position);
                other.node.destroy();
            }
            //撞到道具
            if(other.node.type == 2 || other.node.type == 3){
                    other.node.destroy();
                    //判断飞机是否有僚机 有几架
                    if(self.node.parent.children.length == 2){
                        let wing_plane = window.Global.getClassAndAdd(this.wing_plane,"Wing_plane",this.node.parent);
                        if(this.node.parent.children[1].type == 0){
                            wing_plane.tNode.runAction(cc.moveBy(1,cc.p(60,-10))); 
                            wing_plane.tClass.init(60,1);
                        }else{
                            wing_plane.tNode.runAction(cc.moveBy(1,cc.p(-60,-10))); 
                            wing_plane.tClass.init(-60,0);
                        } 
                   }    
            }
        }
        
        //撞到敌机
        if(other.node.group == "boss"){
            self.node.children[0].active = true; 
            other.node.destroy();
            let bang = window.Global.getClassAndAdd(this.bang,"Bang",self.node.parent.parent); 
            bang.tNode.setPosition(other.node.position);
            this.node.hp  -= 200;
            if(this.node.hp <= 0){
                self.node.destroy();
            }
           
        }

    },


    start () {

    },

    
     update (dt) {
         //刷新血量
        this.node.children[0].getComponent(cc.ProgressBar).progress = this.node.hp / this.HP;
     },
});
